Array.myIsArray = function (obj) {
    return Object.prototype.toString.call(obj) === "[object Array]"
}

console.log(Array.myIsArray([]))

Array.prototype.myMap = function (fn, context) {
    let result = []
    for (let i = 0; i < this.length; i++) {
        result.push(fn.call(context, this[i], i, this))
    }
    return result
}
let a = ([1]).myMap(res => { console.log(res); return res })
console.log("a", a)


Array.prototype.myReduce = function (fn, initValue) {
    let acc = initValue ? initValue : this[0];
    for (let i = 0; i < this.length; i++) {
        acc = fn.call(acc, this[i], i, this)
    }
    return acc
}
let acc = [1, 2, 3].myReduce((pre, next) => (pre + next), 0);
console.log(acc)



Array.prototype.myFlat = function (dep) {
    let result = [];
    if (!dep || dep < 0) return this;
    for (let i = 0; i < this.length; i++) {
        if (Array.isArray(this[i])) {
            let count = dep
            result = result.concat(this[i].myFlat(count-1))
        } else {
            result.push(this[i])
        }

    }
    return result;
}

let res = [1, 2, [3, 4], [1, [2, [3, 4]]]].myFlat(2, []);
console.log(res);

// 去重 排序
Array.from(new Set(arr.flat(Infinity))).sort((a,b)=>{ return a-b})
// isArray
Array.prototype.isArray = function (obj) {
    Object.prototype.toString.call(Object(obj)) === "[Object Array]"
}